 package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.DBtools;
import com.fasterxml.jackson.databind.ObjectMapper;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;




/**
 * Servlet implementation class StataServlet
 */
@WebServlet(description = "更新打卡数据信息，异常备注", urlPatterns = { "/StataServlet","/RemarkSave","/dateremark" })
public class StataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StataServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=UTF-8");
		String TeaId=(String)request.getSession().getAttribute("UserNum");
		List<String> list= new LinkedList<String>();
		
		//初始化得到的参数	  
		String sql="";
		ResultSet rs=null;
		String bool1="";
		String datestring="";
		int [] data=new int[31];
		String name="";
		//JSONArray t= new JSONArray();
		
		Map<Object,Object> map= new HashMap<>();
		
		//得到前台参数
		String [] date =request.getParameterValues("date[]");
		System.out.println("date[0]:"+date[0]);
		System.out.println("date[29]:"+date[29]);
		
		for( int i=1;i<=date.length;i++) {
			sql="select *from teachercheck where TeaId='"+TeaId+"' and left(Date,10)='"+date[i-1]+"'";
			System.out.println("sql:"+sql);
			
			try {
				rs= new DBtools().query(sql);
				
				while(rs.next()) {
					bool1=rs.getString("TeaId");
				}
					
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//判断查找到则为yes,否则no
			if(bool1.length()>0) { date[i-1]="yes"; bool1="";list.add("yes"); data[i-1]=1; name="name"+i;  
			
			 //t.add(i, date[i-1]);
			 map.put(name, date[i-1]);
		}else {
			 date[i-1]="no";bool1="";list.add("no");data[i-1]=0; name="name"+i;// t.add(i, date[i-1]); 
			 map.put(name, date[i-1]);
			
		}		
		
	}
		
		System.out.println("返回的date:"+date[0]);
		System.out.println("返回的date:"+date[10]);
		System.out.println("返回的date:"+date[30]);
		System.out.println("返回的list:"+map.get("name1"));
		System.out.println("返回的list:"+map.get("name29"));
		
		
		//参数转为json格式
		try {
			 // List date=list.addAll(0, list)
			// Object jsonArray =JSONArray.toJSON(data);
			
			//JSONArray t= new JSONArray();t.add(0, t);
			 
			 
			// System.out.println(jsonArray2 instanceof JSONArray);
				
			 //Gson gson =new Gson();
				PrintWriter out =response.getWriter();
				//out.print("{\"aa\":\"aa\",\"bb\":\"bb\"}");
				
			String jsonMap= new ObjectMapper().writeValueAsString(map);	//转换成jackjson
				
				out.print(jsonMap);
				out.flush();
				out.close();
				
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
		
			
		//response.getWriter().append("Served at:1 ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 对未正常打卡人员进行异常备注
		//接收用户数据
		request.setCharacterEncoding("utf-8");
		String TeaId =request.getParameter("TeaId");
		
		String Date = request.getParameter("Date");
		String IsUnusual = request.getParameter("IsUnusual");
		//构造写入数据库sql
		String sql = "update teastatistics set IsUnusual='"+IsUnusual+"' where TeaId ='"+TeaId+"'and Date ='"+Date+"'";	
		 int r = 0;
		try {
			r = new DBtools().execute(sql);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 System.out.println("影响行数："+r); 
		 
		 
		//返回页面
		response.sendRedirect("depcontacts/checkstata.jsp");
		
		
		
		
	}

	/**
	 * @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse)
	 */
	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doDelete(HttpServletRequest, HttpServletResponse)
	 */
	protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doOptions(HttpServletRequest, HttpServletResponse)
	 */
	protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
